package net.zhengxinyun.performance.controller.indicators;

import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.dto.SixTownDTO;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.SixTownEntity;
import net.zhengxinyun.performance.entity.SixIndicatorsEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.SixTownFilter;
import net.zhengxinyun.performance.filter.SixIndicatorsFilter;
import net.zhengxinyun.performance.service.indicators.SixIndicatorsService;
import net.zhengxinyun.performance.service.indicators.SixScoreService;
import net.zhengxinyun.performance.service.indicators.SixTownService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.login.UserService;
import net.zhengxinyun.performance.service.warn.WarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyp1202 on 2018/11/27.
 */
@RestController
@RequestMapping("/sixScore")
public class SixScoreController {
    private SixIndicatorsService sixIndicatorsService;
    private SixTownService sixTownService;
    private TokenService tokenService;
    private SixScoreService sixScoreService;
    private UserService userService;
    private LoginService loginService;

    @Autowired
    private WarnService warnService;

    @Autowired
    public SixScoreController(SixIndicatorsService sixIndicatorsService, SixTownService sixTownService, TokenService tokenService, SixScoreService sixScoreService, UserService userService, LoginService loginService) {
        this.sixIndicatorsService = sixIndicatorsService;
        this.sixTownService = sixTownService;
        this.tokenService = tokenService;
        this.sixScoreService = sixScoreService;
        this.userService = userService;
        this.loginService = loginService;
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> queryScore(@RequestBody SixIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            Double pageTotal = 0.0;
            //返回一级指标和对应的二级指标
            HashMap<String,Object> map = new HashMap<>();
            SixIndicatorsFilter sixIndicatorsFilter = new SixIndicatorsFilter();
            sixIndicatorsFilter.setIndexType("一级指标");
            List<SixIndicatorsFilter> firstIndexList = sixIndicatorsService.query(sixIndicatorsFilter);
            List<Map<String,Object>> indexList = sixIndicatorsService.queryIndexMap(firstIndexList);
            map.put("indexMap",indexList);
            //三个表关联查询出结果
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            if (!"admin".equals(userEntity.getUserPermissions())){
                param.sixIndicatorsFilter.setMainUnit(userEntity.getUserDepartment());
            }
            List<Map<String, Object>> list = sixScoreService.query(param.sixIndicatorsFilter);
            if (list.size() == 0){
                param.sixIndicatorsFilter.setPageNumber(param.sixIndicatorsFilter.getPageNumber()-1);
                list = sixScoreService.query(param.sixIndicatorsFilter);
            }
            map.put("list", list);
            pageTotal = Util.intGetTotalPage(sixScoreService.getThreeTableCount(param.sixIndicatorsFilter),param.sixIndicatorsFilter.getPageSize());
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    /**
     * 审核页面查询
     * @param param
     * @return
     */
    @PostMapping("/queryForAudit")
    public Result<HashMap<String,Object>> queryScoreForAudit(@RequestBody SixIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            Double pageTotal = 0.0;
            //返回一级指标和对应的二级指标
            HashMap<String,Object> map = new HashMap<>();
            SixIndicatorsFilter sixIndicatorsFilter = new SixIndicatorsFilter();
            sixIndicatorsFilter.setIndexType("一级指标");
            List<SixIndicatorsFilter> firstIndexList = sixIndicatorsService.query(sixIndicatorsFilter);
            List<Map<String,Object>> indexList = sixIndicatorsService.queryIndexMap(firstIndexList);
            map.put("indexMap",indexList);
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            if (!"admin".equals(userEntity.getUserPermissions())){
                param.sixIndicatorsFilter.setMainUnit(userEntity.getUserDepartment());
            }
            //三个表关联查询出结果
            List<Map<String, Object>> list = sixScoreService.queryForAudit(param.sixIndicatorsFilter);
            if (list.size() == 0){
                param.sixIndicatorsFilter.setPageNumber(param.sixIndicatorsFilter.getPageNumber()-1);
                list = sixScoreService.queryForAudit(param.sixIndicatorsFilter);
            }
            map.put("list", list);
            pageTotal = Util.intGetTotalPage(sixScoreService.getAuditCount(param.sixIndicatorsFilter),param.sixIndicatorsFilter.getPageSize());
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    /**
     * 设置个乡镇分数
     * sixIndicatorsFilter 接收对应指标id等
     * townList 接收对应乡镇分数排名等
     * @param param
     * @return
     */
    @PostMapping("/setScore")
    public Result<String> setScore(@RequestBody SixScoreParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = sixScoreService.setScore(param.sixIndicatorsFilter,param.list);
            if (!"操作成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }else {
                String s = warnService.updateStatusById(param.sixIndicatorsFilter.getId());
                if (!"预警信息修改成功".equals(s)){
                    return ResultUtils.getResults(Result.Status.ERROR,s);
                }
                return ResultUtils.getSuccessResults();
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/pass")
    public Result<String> pass(@RequestBody SixScoreParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str ;
            TokenEntity tokenEntity = tokenService.queryByValue(param.tokenEntity.getValue());
            UserEntity userEntity = userService.queryById(tokenEntity.getUserId());
            //判断角色权限
            if ("admin".equals(userEntity.getUserPermissions())) {
                str = sixScoreService.auditAllPass(param.list);
                if (!"操作成功".equals(str)){
                    return ResultUtils.getResults(Result.Status.ERROR,str);
                }else {
                    for (SixIndicatorsFilter sixIndicatorsFilter:param.list) {
                        String s = sixScoreService.setFirstFinalScore(sixIndicatorsFilter);
                        if (!"操作成功".equals(s)){
                            return ResultUtils.getResults(Result.Status.ERROR,s);
                        }
                    }
                    return ResultUtils.getSuccessResults();
                }
            }else if ("responsible".equals(userEntity.getUserPermissions())){
                str = sixScoreService.responsibleAllPass(param.list);
                if (!"操作成功".equals(str)){
                    return ResultUtils.getResults(Result.Status.ERROR,str);
                }else {
                    return ResultUtils.getSuccessResults();
                }
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    /**
     * 回退
     * @param param
     * @return
     */
    @PostMapping("/noPass")
    public Result<String> noPass(@RequestBody SixScoreParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = sixScoreService.noPass(param.sixIndicatorsFilter);
            if (!"操作成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }else {
                return ResultUtils.getSuccessResults();
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }

    @PostMapping("/importSixScore")
    public Result<String> importSixScore(@RequestParam(value = "file")MultipartFile file){
        String str = sixScoreService.importSixScore(file);
        if (!"导入成功".equals(str)){
            return ResultUtils.getResults(Result.Status.ERROR,str);
        }
        return ResultUtils.getSuccessResults();
    }
}
class SixScoreParam{
    public SixIndicatorsFilter sixIndicatorsFilter;
    public SixIndicatorsEntity sixIndicatorsEntity;
    public TokenEntity tokenEntity;
    public List<SixTownEntity> townList;
    public SixTownEntity sixTownEntity;
    public SixTownFilter sixTownFilter;
    public List<SixIndicatorsFilter> list;
    public SixTownDTO sixTownDTO;
}
